package com.tzxs.pay.main.goods;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tzxs.pay.R;
import com.tzxs.pay.databinding.ActivityGoodsGatherBinding;
import com.tzxs.pay.main.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品收款
 */
public class GoodsGaActivity extends BaseActivity<ActivityGoodsGatherBinding> {
    private List<String> inList;


    @Override
    protected Activity getChildActivity() {
        return this;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_goods_gather;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIndicator();
        //设置toolbar的导航事件
        dataBinding.goodsGaToolbar.setNavigationOnClickListener(v -> onBackPressed());
        dataBinding.goodsTypeRec.setLayoutManager(new LinearLayoutManager(this));

        GoodsTypeAdapter adapter = new GoodsTypeAdapter();
        adapter.setList(inList);
        dataBinding.goodsTypeRec.setAdapter(adapter);
        adapter.setNavigator((view, type) -> {
            if (type.equals("奶制品")) {
                Snackbar.make(view, "这是奶制品", Snackbar.LENGTH_SHORT).show();
            }
        });

    }

    private void initIndicator() {
        inList = new ArrayList<>();
        inList.add("饮料");
        inList.add("套餐");
        inList.add("百货");
        inList.add("水果");
        inList.add("奶制品");
    }

    class GoodsTypeAdapter extends RecyclerView.Adapter<GoodsTypeAdapter.ViewHolder> {
        private List<String> list;

        private int currentIndext = 0;//当前选择的index

        private void setList(List<String> list) {
            this.list = list;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            return new ViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_goods_type, parent, false));
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {


            holder.name.setText(list.get(position));
            if (holder.getAdapterPosition() == currentIndext) {
                holder.indi.setSelected(true);
                holder.name.setSelected(true);
            } else {
                holder.indi.setSelected(false);
                holder.name.setSelected(false);
            }
            holder.itemView.setOnClickListener(v -> {
                currentIndext = holder.getAdapterPosition();
                navigator.getType(v, list.get(holder.getAdapterPosition()));
                notifyDataSetChanged();
            });


        }


        @Override
        public int getItemCount() {
            return list == null ? 0 : list.size();
        }

        private GoodsTypeNavigator navigator;

        private void setNavigator(GoodsTypeNavigator navigator) {
            this.navigator = navigator;
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            private TextView indi;
            private TextView name;

            private ViewHolder(View itemView) {
                super(itemView);
                indi = itemView.findViewById(R.id.item_goods_type_indicator);
                name = itemView.findViewById(R.id.item_goods_name);
            }
        }

    }
}
